//
//  HMStatusOriginalView.swift
//  CZWeibo31
//
//  Created by apple on 16/8/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
class HMStatusOriginalView: UIView {
    // 原创微博的底部约束记录
    var originalViewBottomConstraint: Constraint?
    // 定义一个ViewModel 供外界赋值
    var statusViewModel: HMStatusViewModel?{
        didSet{
            headImageView.hm_setImageWithString(statusViewModel?.statusModel?.user?.profile_image_url, placeholderImgName: "avatar_default")
            nameLabel.text = statusViewModel?.statusModel?.user?.name
            // 时间和来源 后面处理
            timeLabel.text = statusViewModel?.sinaTimeStr
            sourceLabel.text = statusViewModel?.sinaSourceStr
            // 微博正文
            contentLabel.attributedText = statusViewModel?.orginalAttr
            membershipImageView.image = statusViewModel?.mbrankImage
            avatarImageView.image = statusViewModel?.verifiedImage

            // 卸载约束
            originalViewBottomConstraint?.uninstall()
            // 代表有配图
            if statusViewModel?.statusModel?.pic_urls?.count > 0 {
                // 赋值
                pictureView.pictureInfos = statusViewModel?.statusModel?.pic_urls
                // 设置约束
                self.snp_makeConstraints { (make) -> Void in
                    originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                }
                // 显示配图
                pictureView.hidden = false
            }else {
                // 没有配图
                // 设置约束
                self.snp_makeConstraints { (make) -> Void in
                    originalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                }
                // 隐藏配图
                pictureView.hidden = true
            }
            
        }
    }

    
    // 处理微博正文内容富文本显示
    /*
        A[马到成功]a[笑cry]
    
        a[老王]a[老袁] 字符串  range = [1,4]   range = [6,4]   富文本 a[老王]a[老袁]
        a表a表 富文本
    // 正向遍历失败
         第一次
         a表a[老袁]              富文本a表a[老袁]
         第二次
    // 倒着遍历
        第一次
         a[老王]a表
    */
    func sinaDealContentAttr(text: String) -> NSMutableAttributedString{
        // 把当前的字符串转成富文本
        let allTextAttr = NSMutableAttributedString(string: text)
        // 正则匹配的规则
        let regex = "\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]"
        /**
        *  正则匹配的规则
        *
        *  @param NSString 正则匹配的规则
            - 匹配的个数
            - 匹配的结果
            - 匹配的范围
            - 匹配是否停止
        *
        *  @return
        */
        // 创建一个可变的数组
        var matchArray:[HMMatchModel] = [HMMatchModel]()
        (text as NSString).enumerateStringsMatchedByRegex(regex) { (count, result, range, _) -> Void in
            print(result.memory, range.memory)
            
            // 实例化model
            let model = HMMatchModel(string: (result.memory as! String), range: range.memory)
            // 添加模型到数组中
            matchArray.append(model)
        }
        
        // 倒着遍历数组
        for matchModel in matchArray.reverse() {
            // 判断他是否为nil
            guard let emoticonModel = HMEmoticonTools.sharedTools.searchEmoticonInDefaultEmotionsOrLxhEmoticons(matchModel.string) else{
                continue
            }
            //
            // 创建一个文字附件
            let att = NSTextAttachment()
            // 设置image
            att.image = UIImage.emoticonsImage(emoticonModel)
            // 行高
            let lineHeight = UIFont.systemFontOfSize(15).lineHeight
            // 设置bounds
            att.bounds = CGRect(x: 0, y: -4, width: lineHeight, height: lineHeight)
            // 创建一个不可变的富文本
            let attr = NSAttributedString(attachment: att)
            // 替换
            allTextAttr.replaceCharactersInRange(matchModel.range, withAttributedString: attr)
        }
        
        // 返回一个替换完成后的富文本
        return allTextAttr
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = UIColor.whiteColor()
        // 添加控件
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(membershipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(avatarImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        // 添加约束
        // 01
        headImageView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(10)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        // 02
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(10)
        }
        // 03
        membershipImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(10)
            make.centerY.equalTo(nameLabel)
        }
        // 04
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        
        // 05
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(10)
            make.bottom.equalTo(timeLabel)
        }
        // 06
        avatarImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
            
        }
        // 07
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.leading.equalTo(10)
        }
        
        // 08
        pictureView.snp_makeConstraints { (make) -> Void in
            // 该约束已经放到其内部设置
//            make.size.equalTo(CGSize(width: 100, height: 100))
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.leading.equalTo((SCREENW - 250)/2)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        
        
    }
    
    // MARK: - 懒加载控件
    // 头像
    private lazy var headImageView: UIImageView = UIImageView(imgName: "avatar_default")
    // 名字
    private lazy var nameLabel: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFontOfSize(15)
        lab.textColor = UIColor.darkGrayColor()
        return lab
    }()
    // 用户等级
    private lazy var membershipImageView: UIImageView = UIImageView(imgName: "common_icon_membership")
    // 时间
    private lazy var timeLabel: UILabel = UILabel(textColor: AppThemeColor, textSize: 10)
    // 来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.grayColor(), textSize: 10)
    // 认证
    private lazy var avatarImageView: UIImageView = UIImageView(imgName: "avatar_vgirl")
    // 微博正文
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.blackColor(), textSize: 15, maxWidth: SCREENW - 20)
    // 配图
    private lazy var pictureView: HMStatusPictureView = HMStatusPictureView()
}
